import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jyofservice/fluro/NavigatorUtil.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:jyofservice/jicha/LevelTree.dart';
import 'package:jyofservice/kaoqin/SignList.dart';
import 'package:jyofservice/jicha/JiChaInfo.dart';
import 'package:jyofservice/util/SlideRoute.dart';
import 'package:jyofservice/gongdandiaodu/GongDanDiaoDu.dart';

class RepairerPage extends StatefulWidget {
  RepairerPage({Key? key}) : super(key: key);
  @override
  RepairerPageState createState() => RepairerPageState();
}



class RepairerPageState extends State<RepairerPage> {
  int? receiveNum = 0;
  int? dealNum = 0;
  int? overdueNum = 0;
  int? delayNum = 0;
  int? badgeCount = 0;
  bool isTiXing = true;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getStaticNum();
//    getWoDeListData();
  }

  @override
  Widget build(BuildContext context) {

    return Column(
      children: <Widget>[
//        Container(
//          color: Colors.white,
//          height: ScreenUtil().setHeight(250),
//          child: GridView.count(
//            scrollDirection: Axis.horizontal,
//            crossAxisCount: 1,
//            children: <Widget>[
//              centerColumn_TiXing("images/wodegongdan.png", "我的工单"),
//              centerColumn("images/yichuligongdan.png", "已处理工单"),
////              centerColumn("images/工况信息管理.png", "运行工况"),
//            ],
//          ),
//        ),
//        SizedBox(
//          height: ScreenUtil().setHeight(20),
//        ),
        Container(
          height: ScreenUtil().setHeight(600),
          child: GridView.count(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            crossAxisCount: 2,
            crossAxisSpacing: 1,
            mainAxisSpacing: 1,
            childAspectRatio: 2,
            children: <Widget>[
              bottomSection("images/liubianxing.png", 34, 142, 246, "本月接收工单", receiveNum.toString()+"个"),
              bottomSection("images/liubianxing_2.png", 57, 201, 114, "本月工单处理情况", dealNum.toString()+"个"),
              bottomSection(
                  "images/liubianxing_15.png", 255, 163, 104, "本月工单逾期数量", overdueNum.toString()+"个"),
              bottomSection(
                  "images/liubianxing_33.png", 254, 142, 142, "本月工单缓办数量", delayNum.toString()+"个"),
            ],
          ),
        )
      ],
    );
  }

  TextButton centerColumn_TiXing(String asset, String label) {
    return TextButton(
      onPressed: () {
        if (label == "我的工单") {
          //NavigatorUtil.goGongDanDiaoDu(context, "GetMySelfWaitDoWork", "我的工单");
          Navigator.of(context)
              .push(SlideRoute().createRoute(GongDanDiaoDuPage(
              view: "GetMySelfWaitDoWork", title: "我的工单"))).then((date) {
            getWoDeListData();
          });
//          Navigator.push(context, new MaterialPageRoute(builder: (context) {
//            return GongDanDiaoDuPage(
//                view: "GetMySelfWaitDoWork", title: "我的工单");
//          })).then((date) {
//            getWoDeListData();
//          });
        }
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Stack(
            alignment: Alignment(1.6, -1.0),
            children: <Widget>[
              Image.asset(
                asset,
                width: ScreenUtil().setWidth(120),
                height: ScreenUtil().setHeight(120),
              ),
              Offstage(
                offstage: isTiXing,
                child: Container(
                  alignment: Alignment.center,
                  width: ScreenUtil().setWidth(60),
                  height: ScreenUtil().setWidth(60),
                  decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius:
                    BorderRadius.circular(ScreenUtil().setWidth(30.0)),
                  ),
                  child: Text(
                    '${badgeCount.toString()}',
                    style: TextStyle(
                        color: Colors.white, fontWeight: FontWeight.w500),
                  ),
                ),
              )
            ],
          ),
          SizedBox(
            height: ScreenUtil().setHeight(5),
          ),
          Text(
            label,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30),
              color: Colors.black,
            ),
          )
        ],
      ),
    );
  }

  Container bottomSection(
      String assets, int r, int g, int b, String title, String num) {
    return Container(
      padding: const EdgeInsets.only(top: 10),
      color: Colors.white,
      child: ListTile(
        title: Text(
          title,
          style: TextStyle(
            fontSize: ScreenUtil().setSp(30),
            color: Colors.black54,
          ),
        ),
        subtitle: Text(
          num,
          style: TextStyle(
              fontStyle: FontStyle.italic,
              color: Color.fromRGBO(r, g, b, 1),
              fontSize: ScreenUtil().setSp(40),
              fontWeight: FontWeight.bold),
        ),
        leading: Image.asset(assets),
      ),
    );
  }

  TextButton centerColumn(String asset, String label) {
    return TextButton(
      onPressed: () {
        if (label == "我的工单") {
          Navigator.push(context, SlideRoute().createRoute(GongDanDiaoDuPage(view:"GetMySelfWaitDoWork",title:"我的工单")));
//          NavigatorUtil.goGongDanDiaoDu(context, "GetMySelfWaitDoWork", "我的工单");
        } else if(label=="已处理工单"){
          Navigator.push(context, SlideRoute().createRoute(GongDanDiaoDuPage(view:"GetMyAllFinishWork",title:"已处理工单")));
//          NavigatorUtil.goGongDanDiaoDu(context, "GetMyAllFinishWork", "已处理工单");
        } else if(label=="签到"){
//          NavigatorUtil.goSign(context);
//           Navigator.push(context, SlideRoute().createRoute(SignList()));
        } else if(label=="稽查信息") {
//          NavigatorUtil.goJiCha(context);
          Navigator.push(context, SlideRoute().createRoute(JiChaInfo()));
        }
        else{
          Navigator.push(context, new MaterialPageRoute(builder: (context) {
            return LevelTree();
          }));
        }
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            asset,
            width: ScreenUtil().setWidth(120),
            height: ScreenUtil().setHeight(120),
          ),
          SizedBox(
            height: ScreenUtil().setHeight(5),
          ),
          Text(
            label,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(35),
              color: Colors.black,
            ),
          )
        ],
      ),
    );
  }

  //获取我的工单列表数据
  Future getWoDeListData() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData = await (AesUtil.encodeBase64());
    var data = {
      "key": key,
      "view": 'GetMySelfWaitDoWork',
      "wherestr": '1=1',
//      "limit": 10,
//      "page": 1,
      "sortName": "CreateDate",
      "sortOrder": "desc",
      "ClientType": "phone",
      "ClientKey": aesData
    };
    print(data.toString());

    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/postrowdataforlayui",
        data: data);
    print(response.data.toString());

    if (response.data["code"] == 0) {

      badgeCount = response.data["data"].length;
      if(mounted) {
        setState(() {
          if (badgeCount! > 0) {
            isTiXing = false;
          } else {
            isTiXing = true;
          }
        });
      }

    }
  }

  Future getStaticNum() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData= await (AesUtil.encodeBase64());
    var data = {"key": key,"ClientType":"phone","ClientKey":aesData};
    Response response = await Dio().post(
        WebConfig().baseUrl +
            "Statistics/GetMyDepartmentWorkInfo",
        data: data);
    print(response.data.toString());
    if (response.data["Code"] == "200") {
      if (mounted) {
        setState(() {
          print(response.data["Data"][0]["a"]);
          receiveNum = response.data["Data"][8]["a"];
          dealNum = response.data["Data"][9]["a"];
          overdueNum = response.data["Data"][10]["a"];
          delayNum = response.data["Data"][11]["a"];
        });
      }
    }
  }
}
